import { ternimalRegister } from '@/api/user'
import md5 from 'md5'
import useUserStore from '@/pinia/userInfo'
const isClient = window.electronAPI
const visitorLogin = async (callback) => {
  const userStore = useUserStore()
  if (isClient) {
    try {
      let mac_id = await window.electronAPI.getMacInfo()
      console.log(mac_id)
      const terminal_id = mac_id.replace(/:/g, '-')
      const timestamp = new Date().getTime()
      const sign = md5(terminal_id + String(timestamp))
      const { data } = await ternimalRegister({
        terminal_id,
        timestamp,
        sign
      })
      userStore.setState({
        token: data.token,
        userInfo: {
          ...data
        }
      })
      window.localStorage.removeItem('userLogin')
      callback && callback(true)
    } catch (err) {
      callback && callback(false)
    }
  } else {
    callback && callback(false)
  }
}

export default visitorLogin
